<template>
  <div class="app-container">
    <!-- {{queryParam}}----- -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="100px"
      label-position="left"
    >
      <el-form-item label="计划名称">
        <el-input
          v-model="queryParams.planName"
          placeholder="请输入"
          clearable
          size="small"
          style="width: 160px"
        />
      </el-form-item>
      <el-form-item label="计划部门">
        <el-input
          v-model="queryParams.planDept"
          placeholder="请输入"
          clearable
          size="small"
          style="width: 260px"
        />
      </el-form-item>

      <el-form-item label="计划执行时间">
        <el-date-picker
          v-model="queryParams.planBeginTime"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          type="datetime"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery('queryForm')"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button size="mini" type="primary" @click="add"
          >新增工作按钮</el-button
        >
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="tableData" border>
      <el-table-column prop="id" label="编号"> </el-table-column>
      <el-table-column prop="planName" label="计划名称"> </el-table-column>
      <el-table-column prop="planDeptName" label="计划部门">
        <template slot-scope="scope">
          <span
            v-for="(item, index) in deptOptions[0].children"
            :key="index"
            v-if="scope.row.planDept == item.id && item.id != '1'"
          >
            <span>{{ item.label }}</span>
          </span>
          <span
            v-for="(item, index) in deptOptions"
            :key="index"
            v-if="scope.row.planDept == '1'"
          >
            <span>{{ item.label }}</span>
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="planBeginTime" label="计划执行时间">
        <template slot-scope="scope">
          <span>{{ scope.row.planBeginTime }}</span>
          <span>~</span>
          <span>{{ scope.row.planEndTime }}</span>
        </template>
      </el-table-column>

      <el-table-column prop="planEvaluate" label="评定分数类型">
        <template slot-scope="scope">
          <span v-if="scope.row.planEvaluate == 0">十分制</span>
          <span v-else-if="scope.row.planEvaluate == 1">百分制</span>
          <span v-else-if="scope.row.planEvaluate == 2">优良差</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            circle
            @click="upa(scope.row)"
          ></el-button>
          <el-button
            v-if="scope.row.id !== 1"
            size="mini"
            circle
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:workPlan:remove']"
          />
        </template>
      </el-table-column>
    </el-table>

    <pagination
      style="float: right; margin-right: 80px"
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog
      title="新增工作计划"
      :visible.sync="dialogVisible"
      width="60%"
      @close="cancel('queryParamAdd')"
    >
      <el-form :model="queryParam" :rules="rules" ref="queryParamAdd">
        <el-row>
          <el-col :span="12">
            <el-form-item label="计划名称" label-width="120px" prop="planName">
              <el-input
                v-model="queryParam.planName"
                placeholder="请输入"
                clearable
                size="small"
                style="width: 230px"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="计划部门"
              label-width="90px"
              prop="planDeptName"
            >
              <treeselect
                v-model="queryParam.planDept"
                :options="deptOptions"
                placeholder="请选择归属部门"
                style="width: 240px"
                @select="selectDepart"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="计划时间" label-width="120px">
              <el-date-picker
                v-model="dateRange"
                size="small"
                style="width: 230px"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                @change="jhsjChange"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划类型" label-width="90px" prop="planType">
              <el-select
                placeholder="请选择"
                size="small"
                style="width: 240px"
                v-model="queryParam.planType"
              >
                <el-option
                  v-for="dict in plantypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item
              label="计划描述"
              label-width="120px"
              prop="evaluateResult"
            >
              <el-input
                v-model="queryParam.planDesc"
                placeholder="请输入"
                clearable
                size="small"
                style="width: 230px"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划执行人" label-width="95px" prop="userName">
              <el-select
                multiple
                placeholder="请选择"
                style="width: 240px"
                size="small"
                v-model="queryParam.userName"
                @change="jhcxrChange"
              >
                <el-option
                  v-for="item in execution"
                  :key="item.username"
                  :label="item.username"
                  :value="item.username"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item
              label="计划评定标准"
              label-width="120px"
              prop="planEvaluate"
            >
              <el-select
                v-model="queryParam.planEvaluate"
                placeholder="请选择"
                size="small"
                style="width: 230px"
              >
                <el-option
                  v-for="dict in planEvaluationcriteriaOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12"></el-col>
        </el-row>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="determine('queryParamAdd')"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <el-dialog
      title="修改工作计划"
      :visible.sync="dialogVisible1"
      width="60%"
      @close="cancel('queryParamUpdate')"
    >
      <el-form :model="form1" :rules="rules" ref="queryParamUpdate">
        <el-row>
          <el-col :span="12">
            <el-form-item label="计划名称" label-width="90px" prop="planName">
              <el-input
                v-model="form1.planName"
                placeholder="请输入"
                clearable
                size="small"
                style="width: 210px"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划部门" label-width="90px" prop="planDept">
              <treeselect
                v-model="form1.planDept"
                :options="deptOptions"
                placeholder="请选择归属部门"
                style="width: 240px"
                @select="selectDepart"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="计划时间" label-width="90px">
              <el-date-picker
                v-model="dateRange"
                size="small"
                style="width: 210px"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划类型" label-width="90px" prop="planType">
              <el-select
                placeholder="请选择"
                size="small"
                style="width: 240px"
                v-model="form1.planType"
              >
                <el-option
                  v-for="dict in plantypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item
              label="计划描述"
              label-width="90px"
              prop="planDescription"
            >
              <el-input
                v-model="form1.planDesc"
                placeholder="请输入"
                clearable
                size="small"
                style="width: 210px"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- {{form1.userName}} -->
            <el-form-item label="计划执行人" label-width="95px" prop="userName">
              <el-select
                multiple
                placeholder="请选择"
                style="width: 240px"
                size="small"
                v-model="queryParam.userName"
                @change="jhcxrChange"
              >
                <el-option
                  v-for="item in execution"
                  :key="item.username"
                  :label="item.username"
                  :value="item.username"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item
              label="计划评定标准"
              label-width="100px"
              prop="planEvaluate"
            >
              <el-select v-model="form1.planEvaluate" placeholder="请选择">
                <el-option
                  v-for="dict in planEvaluationcriteriaOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12"></el-col>
        </el-row>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="determine('queryParamUpdate')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// 例如：import 《组件名称》 from '《组件路径》';
import {
  modelList,
  // FuWu,
  removeProcess,
  ZhiXing,
  updateUser,
  addUser,
} from "@/api/workflow/workplan.js";
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  // import引入的组件需要注入到对象中才能使用
  components: {
    Treeselect,
  },
  data() {
    // 这里存放数据
    return {
      dialogVisible1: false,
      state4: "",
      execution: [],
      planEvaluationcriteriaOptions: [],
      plantypeOptions: [],
      deptOptions: [],
      jhsj: [],
      form1: {
        createBy: "",
        createTime: "",
        doneRatio: "",
        evaluateResult: "",
        id: undefined,
        modifyBy: "",
        modifyTime: "",
        pageNum: 0,
        pageSize: 0,
        planBeginTime: "",
        planDept: undefined,
        planEndTime: "",
        planEvaluate: "",
        planId: 0,
        planName: "",
        planType: "",
        userName: [],
        workPlanPersonList: [],
        planDesc: "",
        planDeptName: "",
      },

      dateRange: [],
      total: 0,
      loading: true,

      dialogVisible: false,
      tableData: [],
      queryParam: {
        createBy: "",
        createTime: "",
        doneRatio: "",
        evaluateResult: "",
        id: undefined,
        modifyBy: "",
        modifyTime: "",
        pageNum: 0,
        pageSize: 0,
        planBeginTime: "",
        planDept: undefined,
        planEndTime: "",
        planEvaluate: "",
        planId: 0,
        planName: "",
        planType: "",
        userName: [],
        workPlanPersonList: [],
        planDesc: "",
        planDeptName: "",
      },
      // 查询参数
      ArrList: {
        createBy: "",
        createTime: "",
        doneRatio: "",
        evaluateResult: "",
        id: undefined,
        modifyBy: "",
        modifyTime: "",
        pageNum: 0,
        pageSize: 0,
        planBeginTime: "",
        planDept: undefined,
        planEndTime: "",
        planEvaluate: "",
        planId: 0,
        planName: "",
        planType: "",
        userName: [],
        planDeptName: "",
      },
      workPlanPersonList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planBeginTime: null,
        planDept: undefined,
        planEndTime: null,
        planEvaluate: null,
        planId: null,
        planName: null,
        planType: null,
        userName: null,
      },
      jhcxr: [],
      userName: [],
      workPlanPersonList: [],
      item: {
        planType: "", //计划类型
        planExecutor: "", //计划执行人
        planDescription: "", //计划描述
        plannedExecutiontime: "", //计划时间
        // planningDepartment: "", //计划部门
        planName: "", //计划名称
        planEvaluationcriteria: "", //计划评定标准
        planDeptName: "", //计划部门
      },
      zxren: {},
      statusOptions: [],
      form: {},
      defaultProps: {
        children: "children",
        label: "label",
      },
      rules: {
        planName: [
          { required: true, message: "请输入计划名称", trigger: "blur" },
        ],
      },
    };
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 方法集合
  methods: {
    reset() {
      this.queryParam = {
        createBy: "",
        createTime: "",
        doneRatio: "",
        evaluateResult: "",
        id: undefined,
        modifyBy: "",
        modifyTime: "",
        planBeginTime: "",
        planDept: undefined,
        planEndTime: "",
        planEvaluate: "",
        planId: 0,
        planName: "",
        planType: "",
        userName: [],
        workPlanPersonList: [],
        planDesc: "",
        // planDeptName:'',
      };
      this.form1 = {
        createBy: "",
        createTime: "",
        doneRatio: "",
        evaluateResult: "",
        id: undefined,
        modifyBy: "",
        modifyTime: "",
        planBeginTime: "",
        planDept: undefined,
        planEndTime: "",
        planEvaluate: "",
        planId: 0,
        planName: "",
        planType: "",
        userName: [],
        workPlanPersonList: [],
        planDesc: "",
        // planDeptName:'',
      };
      this.resetForm("queryParamAdd");
      this.dateRange = [];
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      (this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        planBeginTime: null,
        planDept: undefined,
        planEndTime: null,
        planEvaluate: null,
        planId: null,
        planName: null,
        planType: null,
        userName: null,
      }),
        this.getList();
    },
    cancel(form) {
      // alert(form)
      (this.dateRange = []), this.$refs[form].resetFields();
    },
    add() {
      this.reset();
      this.executionss();
      this.dialogVisible = true;
    },
    jhcxrChange(val) {
      console.log(val);

      this.jhcxr = val;
    },
    jhsjChange(val) {
      console.log(val);
      val.forEach((arr, index) => {
        console.log(arr, index);
        if (index == 0) {
          this.queryParam.planBeginTime = arr;
        } else if (index == 1) {
          this.queryParam.planEndTime = arr;
        }
      });
    },
    executionss() {
      var obj = {
        pageNum: 1,
        pageSize: 100,
        planBeginTime: null,
        planDept: undefined,
        planEndTime: null,
        planEvaluate: null,
        planId: null,
        planName: null,
        planType: null,
        userName: null,
        // planDeptName:null,
      };
      //计划执行人操作
      ZhiXing(obj).then((response) => {
        this.execution = response.data.list;
        console.log(this.execution);
      });
    },

    //重置操作
    upa(row) {
      console.log("upa()", row);
      this.form1 = row;
      this.executionss();
      if (this.userName) {
        this.queryParam.userName = this.userName;
        this.form1.workPlanPersonList = this.workPlanPersonList;
      }
      console.log(this.form1, "form1");
      this.dateRange = [this.form1.planBeginTime, this.form1.planEndTime];
      console.log(this.form1);
      this.dialogVisible1 = true;

      console.log(this.planDescription, "99999");
    },
    getTreeselect() {
      treeselect().then((response) => {
        this.deptOptions = response.data;
      });
    },

    querySearchAsync(queryString, cb) {
      //调接口
      console.log(queryString);
      var results = null;
      cb(results);
    },
    createStateFilter(queryString) {
      return (state) => {
        return (
          state.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0
        );
      };
    },
    handleSelect(item) {
      console.log(item);
    },
    getrTreeselect() {
      treeselect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    selectDepart(val) {
      this.form1.planDeptName = val.label;
      this.queryParam.planDeptName = val.label;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const planId = row.id || this.ids;
      this.$confirm('是否确认删除用户编号为"' + planId + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return removeProcess(row);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },

  
    getList() {
      this.loading = true;
      modelList(this.queryParams).then((response) => {
        console.log(response, "134");
        this.tableData = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
      console.log(this.tableData, "134");
    },

    determine(queryParam) {
      //点击确定出现校验

      this.$refs[queryParam].validate((valid) => {
        if (valid) {
          if (this.form1.id != null) {
            const userNameList = this.jhcxr;
            let userNameObjList = [];
            userNameList.forEach((arr, index) => {
              let a = {
                userName: arr,
              };
              userNameObjList.push(a);
            });
            console.log(userNameObjList, "userNameObjList");
            this.form1.workPlanPersonList = userNameObjList;
            // console.log(this.fomr1.workPlanPersonList,'workPlanPersonList');
            this.form1.userName = this.queryParam.userName;
            updateUser(this.form1).then((response) => {
              this.$message({
                message: "修改成功",
                type: "success",
              });
              if (response.code == 200) {
                this.workPlanPersonList = this.form1.workPlanPersonList;
                this.userName = this.form1.userName;
              }
              this.dialogVisible1 = false;
              this.getList();
            });
          } else {
            console.log(this.jhcxr);
            // this.queryParam.userName=this.jhcxr.join(',');
            const userNameList = this.jhcxr;
            let userNameObjList = [];
            userNameList.forEach((arr, index) => {
              let a = {
                userName: arr,
              };
              userNameObjList.push(a);
            });
            this.queryParam.workPlanPersonList = userNameObjList;
         

            addUser(this.queryParam).then((response) => {
              this.$message({
                message: "添加成功",
                type: "success",
              });
              if (response.code == 200) {
                this.workPlanPersonList = this.queryParam.workPlanPersonList;
                this.userName = this.queryParam.userName;
              }
              this.dialogVisible = false;
              this.getList();
            });
          }
        }
      });
    },
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
    // this.getFw();
    this.getList();
    this.getDicts("plan_type").then((response) => {
      this.plantypeOptions = response.data;
    });
    this.getDicts("plan_evaluate_type").then((response) => {
      this.planEvaluationcriteriaOptions = response.data;
    });
    this.getTreeselect();
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {}, // 如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style>
</style>
